package cn.sdack.go.users.controller;

import cn.sdack.go.common.entities.BaseEntity;
import cn.sdack.go.common.entities.JsonResult;
import cn.sdack.go.common.entities.users.DictEntity;
import cn.sdack.go.common.entities.users.DictItemEntity;
import cn.sdack.go.users.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author sdack
 * @date 2024/2/17
 */
@RestController
public class DictController {

    @Autowired
    private DictService dictService;


    @GetMapping("/dict/{code}")
    public JsonResult<?> getByCode(@PathVariable("code") String code) {
        try {
            DictEntity entity = dictService.getByCode(code);
            return JsonResult.toJson(entity);
        } catch (Exception e) {
            String message = e.getMessage().toString();
            if (message.length() > 100) {
                message = message.substring(0, 100);
            }
            return JsonResult.toJson(false, message);
        }
    }

    @GetMapping("/dict/item/{id}")
    public JsonResult<?> getById(@PathVariable("id") Long id) {
        try {
            DictItemEntity entity = dictService.getByCode(id);
            return JsonResult.toJson(entity);
        } catch (Exception e) {
            String message = e.getMessage().toString();
            if (message.length() > 100) {
                message = message.substring(0, 100);
            }
            return JsonResult.toJson(false, message);
        }
    }

    @PostMapping("/dict")
    public JsonResult<?> add(@RequestBody @Validated(BaseEntity.Add.class) DictEntity param, BindingResult errors) {
        if (errors.hasErrors()) {
            return JsonResult.toJson(false, errors.getAllErrors().get(0).getDefaultMessage());
        }
        try {
            DictEntity entity = dictService.add(param);
            return JsonResult.toJson(entity);
        } catch (Exception e) {
            String message = e.getMessage().toString();
            if (message.length() > 100) {
                message = message.substring(0, 100);
            }
            return JsonResult.toJson(false, message);
        }
    }


    @PostMapping("/dict/item")
    public JsonResult<?> addItem(@RequestBody @Validated(BaseEntity.Add.class) DictItemEntity param, BindingResult errors) {
        if (errors.hasErrors()) {
            return JsonResult.toJson(false, errors.getAllErrors().get(0).getDefaultMessage());
        }
        try {
            DictItemEntity entity = dictService.addItem(param);
            return JsonResult.toJson(entity);
        } catch (Exception e) {
            String message = e.getMessage().toString();
            if (message.length() > 100) {
                message = message.substring(0, 100);
            }
            return JsonResult.toJson(false, message);
        }
    }


    @PutMapping("/dict")
    public JsonResult<?> edit(@RequestBody @Validated(BaseEntity.Set.class) DictEntity param, BindingResult errors) {
        if (errors.hasErrors()) {
            return JsonResult.toJson(false, errors.getAllErrors().get(0).getDefaultMessage());
        }
        try {
            DictEntity entity = dictService.edit(param);
            return JsonResult.toJson(entity);
        } catch (Exception e) {
            String message = e.getMessage().toString();
            if (message.length() > 100) {
                message = message.substring(0, 100);
            }
            return JsonResult.toJson(false, message);
        }
    }


    @PutMapping("/dict/item")
    public JsonResult<?> editItem(@RequestBody @Validated(BaseEntity.Set.class) DictItemEntity param, BindingResult errors) {
        if (errors.hasErrors()) {
            return JsonResult.toJson(false, errors.getAllErrors().get(0).getDefaultMessage());
        }
        try {
            DictItemEntity entity = dictService.editItem(param);
            return JsonResult.toJson(entity);
        } catch (Exception e) {
            String message = e.getMessage().toString();
            if (message.length() > 100) {
                message = message.substring(0, 100);
            }
            return JsonResult.toJson(false, message);
        }
    }


    @GetMapping("/dict/status/{code}")
    public JsonResult<?> setStatus(@PathVariable("code") String code, @RequestParam("status") Integer status) {
        try {
            DictEntity entity = dictService.status(code,status);
            return JsonResult.toJson(entity);
        } catch (Exception e) {
            String message = e.getMessage().toString();
            if (message.length() > 100) {
                message = message.substring(0, 100);
            }
            return JsonResult.toJson(false, message);
        }
    }

    @GetMapping("/dict/item/status/{id}")
    public JsonResult<?> setStatusItem(@PathVariable("id") Long id, @RequestParam("status") Integer status) {
        try {
            DictItemEntity entity = dictService.statusItem(id,status);
            return JsonResult.toJson(entity);
        } catch (Exception e) {
            String message = e.getMessage().toString();
            if (message.length() > 100) {
                message = message.substring(0, 100);
            }
            return JsonResult.toJson(false, message);
        }
    }

    @DeleteMapping("/dict/{code}")
    public JsonResult<?> del(@PathVariable("code") String code) {
        try {
            dictService.del(code);
            return JsonResult.toJson(true);
        } catch (Exception e) {
            String message = e.getMessage().toString();
            if (message.length() > 100) {
                message = message.substring(0, 100);
            }
            return JsonResult.toJson(false, message);
        }
    }

    @DeleteMapping("/dict/item/{id}")
    public JsonResult<?> delItem(@PathVariable("id") Long id) {
        try {
            dictService.delItem(id);
            return JsonResult.toJson(true);
        } catch (Exception e) {
            String message = e.getMessage().toString();
            if (message.length() > 100) {
                message = message.substring(0, 100);
            }
            return JsonResult.toJson(false, message);
        }
    }

}
